

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @(#)TicTacToe.java 1.5
 * Tryggvi Geir Magnússon
 * Date: 12.11.2011
 * Time: 11:5--2
 */
        //Fyrirlestur

public class TicTacToe implements ActionListener {
    private JFrame game = new JFrame("Silver bullets awesome Tic Tac Toe game!");
    private JButton buttons[] = new JButton[9];
    private int counter = 0;
    private ImageIcon x = new ImageIcon(getClass().getResource("/X.png"));
    private ImageIcon o = new ImageIcon(getClass().getResource("/O.png"));
    private boolean winner = false;
    private boolean testing = false;
    private boolean tester = false;
    private boolean closingWindow = false;


    public void setClosingWindow(boolean closingWindow) {
        this.closingWindow = closingWindow;
    }

    public boolean isClosingWindow() {
        return closingWindow;
    }

    public boolean getTesting() {
        return testing;
    }

    public void setTesting(boolean testingVar) {
        testing = testingVar;
    }

    public boolean getTester() {
        return tester;
    }

    public void setTester(boolean testerVar) {
        tester = testerVar;
    }

    public ImageIcon getO() {
        return o;
    }

    public ImageIcon getX() {
        return x;
    }

    public int getNumberOfButtons() {
        return buttons.length;

    }

    public JFrame getFrame() {
        return game;
    }


    public JButton getButton(int i) {
        return buttons[i];
    }

    public boolean getIcon(String icon) {
        ImageIcon img = new ImageIcon();
        if (icon.equals("x")) {
            img = getX();
        }
        if (icon.equals("y")) {
            img = getO();
        }
        if (img == null) {
            return false;
        }
        return true;

    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counterVar) {
        counter = counterVar;
    }


    public void setButtons(JButton button, int id) {
        buttons[id] = button;
    }

    public void windowClosed(WindowEvent e) {
        game.dispose();
        System.exit(0);
    }

    public TicTacToe() {

        game.setSize(400, 400);                               //Stærð á glugga
        game.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  // Hættir þegar lokað er glugganum
        game.setLayout(new GridLayout(3, 3));    // 3*3 = 9, sem eru þá allir takkarnir

        create();
    }

    public static void main(String[] args) {

        TicTacToe main = new TicTacToe();
        if (args.length != 0) {
            main.setClosingWindow(true);
            JButton j = new JButton();
            ActionEvent e = new ActionEvent(j, 0, null);
            main.actionPerformed(e);
        }
        if (main.getTesting()) {
            main.game.setVisible(false);                                // Sjá til þess að glugginn sjáist
        }
        else
        {
            main.game.setVisible(true);
        }
    }


    private int[][] possibleWins = new int[][]{
            {0, 1, 2}, {3, 4, 5}, {6, 7, 8},
            {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
            {0, 4, 8}, {2, 4, 6}
    };

    public boolean create() {


        for (int i = 0; i <= 8; i++) {
            buttons[i] = new JButton();
            game.add(buttons[i]);
            buttons[i].addActionListener(this);
            buttons[i].setName("" + i);
        }

        if (game != null) {
            return true;
        }
        return false;
    }

    public boolean delete() {

        GridLayout grid = new GridLayout(3, 3);
        game.setLayout(grid);
        create();

        if (grid.getRows() != 0) {
            return true;
        }
        return false;
    }

    public boolean isVisible() {
        return game.isVisible();

    }


    public void actionPerformed(ActionEvent e) {

        JButton button = (JButton) e.getSource();

        if (closingWindow) {
            game.dispose();
            System.exit(0);
        }

        if (counter % 2 == 0) {
            button.setIcon(x);
            button.setName("x");
            button.setEnabled(false);
            counter++;
        } else {
            button.setIcon(o);
            button.setName("o");
            button.setEnabled(false);
            counter++;
        }


        for (int i = 0; i <= 7; i++) {
            if (buttons[possibleWins[i][0]].getName().equals(buttons[possibleWins[i][1]].getName())
                    && buttons[possibleWins[i][1]].getName().equals(buttons[possibleWins[i][2]].getName())
                    && buttons[possibleWins[i][0]].getName() != "") {
                winner = true;
            }
        }

        if (winner) {
            if (counter % 2 == 0) {
                if (!testing) {
                    JOptionPane.showMessageDialog(null, "Player O wins!");
                    game.dispose();
                    System.exit(0);
                } else {

                    tester = true;
                }
            } else {
                if (!testing) {
                    JOptionPane.showMessageDialog(game, "Player X wins!");
                    game.dispose();
                    System.exit(0);
                } else {
                    tester = true;
                }
            }
        } else if (counter == 9) {
            if (testing) {
                tester = true;
            } else {
                JOptionPane.showMessageDialog(game, "The game was a tie!");
                game.dispose();
                System.exit(0);
            }
        }


    }
}


